package com.dinner.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dinner.auto.JSONAuthentication;
import com.dinner.entity.DiCar;
import com.dinner.entity.DiOrder;
import com.dinner.entity.DiUser;
import com.dinner.entity.OrderMenu;
import com.dinner.mapper.DiOrderMapper;

import com.dinner.mapper.OrderMenuMapper;
import com.dinner.response.Result;
import com.dinner.service.DiOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dinner
 * @since 2022-02-13
 */
@RestController
@RequestMapping("/di-order")
public class DiOrderController extends JSONAuthentication {
    @Autowired
    DiOrderMapper diOrderMapper;
    @Autowired
    DiOrderService diOrderService;
    @ApiOperation("添加订单")
    @PostMapping("/add")
    public void addSeat(HttpServletRequest request, HttpServletResponse response, @RequestParam("orderPrice") float orderPrice,@RequestParam("seatId") int seatId,@RequestParam("userId") int userId) throws Exception{
        DiOrder diOrder=new DiOrder();
        diOrder.setOrderPrice(orderPrice);
        diOrder.setSeatId(seatId);
        diOrder.setUserId(userId);
        if(diOrderMapper.insert(diOrder)>0){
            Result ok = Result.ok().data("orderId",diOrder.getId());
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改订单状态通过订单id")
    @PostMapping("/updOrderByid")
    public void updOrder(HttpServletRequest request, HttpServletResponse response,@RequestParam("orderStatus") int orderStatus,@RequestParam("id") int id)throws  Exception {
        DiOrder diOrder=diOrderMapper.selectById(id);
        diOrder.setOrderStatus(orderStatus);
        if(diOrderMapper.updateById(diOrder)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看所有定单")
    @PostMapping("/lookOrder")
    public void lookMenu(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size ,@RequestParam("current") int current) throws Exception{

        List<DiOrder> list=diOrderService.findOrderAll(current,size);
        int total =diOrderMapper.selectCount(null);
        if(list.size()>0){
            Result ok = Result.ok().data("menu",list).data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("暂无订单");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看定单通过用户id")
    @PostMapping("/lookMenuByUserId")
    public void lookMenuByUserId(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size ,@RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception{
            QueryWrapper  wrapper=new QueryWrapper();
            wrapper.eq("user_id",userId);
        List<DiOrder> list=diOrderService.findOrderByUserId(current,size,userId);
        int total =diOrderMapper.selectCount(wrapper);
        if(list.size()>0){
            Result ok = Result.ok().data("menu",list).data("total",total);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("暂无订单");
            this.WriteJSON(request, response, error);
        }
    }
}

